/*
 * Copyright (c)  2018, XiQiao
 * All rights reserved. 
 *
 * Id:RegexUtil.java   2018-07-27 14:11 wanglijun
 */
package io.marmot.poi.util;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2018
 * </p>
 * <p>
 * Company: XiQiao
 * </p>
 *
 * @author: wanglijun
 * @create: 2018-07-27 14:11
 * @version：1.0
 */
public class RegexUtil {

    private RegexUtil() {}

    private final static LoadingCache<String, Pattern> regexPatternLoadingCache =
            CacheBuilder.newBuilder()
                    .maximumSize(5)
                    .build(new CacheLoader<String, Pattern> () {

                        @Override
                        public Pattern load(String pattern) {
                            return Pattern.compile(pattern);
                        }
                    });

    public static Boolean isMatches(String pattern, Object value) {
        try {
            String valueString = (String) value;
            return regexPatternLoadingCache.get(pattern).matcher(valueString).matches();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return false;
    }
}
